KM.OBJLoader = function() {};

KM.OBJLoader.prototype = {
    
    constructor: KM.OBJLoader,
    
    load: function( iURI, iOnLoad, iOnProgress, iOnError )
    {
        this._onLoad = iOnLoad;
        this._onProgress = iOnProgress;
        this._onError = iOnError;
        
        var request = new XMLHttpRequest();
        
        request.addEventListener( 'load', this.onLoad.bind( this ) );
        request.addEventListener( 'progress', this.onProgress.bind( this ) );
        request.addEventListener( 'error', this.onError.bind( this ) );
        
        request.open( 'GET', iURI, true );
        request.send( null );
    },
    
    onLoad: function( iEvent )
    {
        var response = iEvent.target.responseText;
        
        var oGeometry = this.parse( response );
        
        this._onLoad( oGeometry );
    },
    
    onProgress: function( iEvent )
    {
        var iPercent = ( iEvent.loaded / iEvent.total ) * 100;
        this._onProgress( iPercent );
    },
    
    onError: function( iEvent )
    {
        this._onError( iEvent );
    },
    
    parse: function( iOBJData )
    {
        var oGeometry = new KM.Geometry();
        
        var iLines = iOBJData.split( '\n' );
        var iLinesNb = iLines.length;
        for( var i = 0; i < iLinesNb; i++ )
        {
            var iLine = iLines[ i ];
            iLine.trim();
            
            var iLineLength = iLine.length;
            var iLineFirstChar = iLine.charAt( 0 );
            if( iLineLength === 0 || iLineFirstChar === "#" || iLineFirstChar === "g" ) continue;
//            console.log("OBJLoader.js, parse() : iLine_" + i + ", : " + iLine);

            // Vertex && Normal
            if( iLineFirstChar === "v" )
            {
                var iLineSecondChar = iLine.charAt( 1 );
                
                // Normal
                if( iLineSecondChar == "n" )
                {
                    var iLineParts = iLine.split( " " );
                    var vnx = iLineParts[ 2 ];
                    var vny = iLineParts[ 4 ];
                    var vnz = iLineParts[ 6 ];
//                    console.log("OBJLoader.js, parse() : vn( " + vnx + ", " + vny + ", " + vnz + " )" );
                    oGeometry.addNormal( vnx, vny, vnz );
                }
                // Vertex
                else
                {
                    var iLineParts = iLine.split( " " );
                    var vx = parseFloat( iLineParts[ 2 ] );
                    var vy = parseFloat( iLineParts[ 4 ] );
                    var vz = parseFloat( iLineParts[ 6 ] );
//                    console.log("OBJLoader.js, parse() : v( " + vx + ", " + vy + ", " + vz + " )" );
                    oGeometry.addVertex( vx, vy, vz );
                }
            }
            else if( iLineFirstChar === "f" )
            {
                var iLineParts = iLine.split( " " );
                var fa = parseFloat( iLineParts[ 2 ] );
                var fb = parseFloat( iLineParts[ 4 ] );
                var fc = parseFloat( iLineParts[ 6 ] );
//                console.log("OBJLoader.js, parse() : f( " + fa + ", " + fb + ", " + fc + " )" );
                oGeometry.addTriangle( fa - 1, fb - 1, fc - 1 );
            }
        }
        
        return oGeometry;
    },
};